/**
 * 
 */
package database.local;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import server.SaveZoneHttpConnection;
import server.ServerObject;
import server.XmlParser;

/**
 * @author shiko
 *
 */
public class Business extends ServerObject implements Comparable<Business> {
	private String name;
	private String country;
	private String city;
	private String street;
	private int streetNum;
	private String fullAddress;
	private String type; /* pub or rest*/
	private String website;
	private String pic_link;
	private String more_info;
	private Integer latitude;
	private Integer longitude;
	private List<Deal> deals;
	
	public Business() {
		super();
		setName("");
		setCity("");
		setStreet("");
		setStreetNum(0);
		setFullAddress("");
		setType("");
		setWebsite("");
		setCountry("");
		setPic_link("");
		setDeals(new ArrayList<Deal>());
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public int getStreetNum() {
		return streetNum;
	}

	public void setStreetNum(int streetNum) {
		this.streetNum = streetNum;
	}

	public String getFullAddress() {
		
		if (fullAddress == null || fullAddress == ""){
			fullAddress = "";
			
			if (getCountry() != null){
				fullAddress += getCountry();
				fullAddress += " ";
			}
			
			if (getCity() != null){
				fullAddress += getCity();
				fullAddress += " ";
			}
			
			if (getStreet() != null) {
				fullAddress += getStreet();
				fullAddress += " ";
			}
			
			if (getStreetNum() > 0) {
				fullAddress += Integer.toString(getStreetNum());
				fullAddress += " ";
			}
			
		}
		
		return fullAddress.trim();
	}

	public void setFullAddress(String fullAddress) {
		this.fullAddress = fullAddress;
	}

	public Integer getLatitude() {
		return latitude;
	}

	public void setLatitude(Integer latitude) {
		this.latitude = latitude;
	}

	public Integer getLongitude() {
		return longitude;
	}

	public void setLongitude(Integer longitude) {
		this.longitude = longitude;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	/**
	 * @return the more_info
	 */
	public String getMore_info() {
		return more_info;
	}

	/**
	 * @param more_info the more_info to set
	 */
	public void setMore_info(String more_info) {
		this.more_info = more_info;
	}

	/**
	 * 
	 * @return sorted list by rating of deals.
	 */
	public List<Deal> getDeals() {
		if (deals == null) return null;
		Collections.sort(deals);
		return deals;
	}

	public void setDeals(List<Deal> deals) {
		this.deals = deals;
	}
	
	public String toXml() {
		return XmlParser.businessToXml(this);
	}
	
	public void checkInMe(String user_name, String email, String pic, String facebook_link) {
		SaveZoneHttpConnection.checkIn(this, user_name, email, pic, facebook_link);
	}
	
	/*compared by rating of the max deal rate*/
	public int compareTo(Business another) {
		if (another == null) return -1;
		Deal this_deal = this.getDealMaxRate();
		Deal another_deal = another.getDealMaxRate();
		if (this_deal == null) return 1;
		if (another_deal == null) return -1;
		int compare = this_deal.compareTo(another_deal);
		return compare;
	}
	
	/**
	 * 
	 * @return the deal with the max rate.
	 * if there is no deal return null.
	 */
	public Deal getDealMaxRate() {
		List<Deal> deals = getDeals();
		if (deals == null || deals.size() == 0) return null;
		Collections.sort(deals);
		return deals.get(0);
	}
	
	/**
	 * used to add or to update the remote data base  
	 * @return false if failed
	 * @param user can be changed to user_name
	 * 
	 */
	public boolean syncWithMainDataBase(User user) {
		List<Business> business_lst = new LinkedList<Business>();
		business_lst.add(this);
		InputStream stream = SaveZoneHttpConnection.add(business_lst, user);
		
		if (stream == null) return false;
		
		/*check if failed*/
		return SaveZoneHttpConnection.checkResult(stream);
	}
	

	public String getPic_link() {
		return pic_link;
	}

	public void setPic_link(String pic_link) {
		this.pic_link = pic_link;
	}

}
